package com.itheima.day09泛型和二叉树;

import java.util.Comparator;
import java.util.TreeSet;

/*3、【编号：1506】在某次考试中，
学生的成绩信息如下： 姓名 年龄 成绩
Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100
Lucy 22 90 Kevin 22 90
请分别用Comparable和Comparator
两个接口对以上同学的成绩做降序排序，
如果成绩一样，
那在成绩排序的基础上按照年龄由小到大排序，
成绩和年龄都一样，则按照姓名的字典顺序排序。*/
public class Demo3 {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<Student>();
        set.add(new Student("Tom", 20, 90));
        set.add(new Student("Jerry", 22, 95));
        set.add(new Student("Lily", 20, 100));
        set.add(new Student("Lucy", 22, 90));
        set.add(new Student("Kevin", 22, 90));
        for (Student student : set) {
            System.out.println(student);
        }
        System.out.println("..................");
        TreeSet<Student> set1 = new TreeSet<Student>((Student o1,Student o2)->{int x = o2.getScore() - o1.getScore();
            return x == 0 ? o1.getName().compareTo(o2.getName()) : x;});
        set1.add(new Student("Tom", 20, 90));
        set1.add(new Student("Jerry", 22, 95));
        set1.add(new Student("Lily", 20, 100));
        set1.add(new Student("Lucy", 22, 90));
        set1.add(new Student("Kevin", 22, 90));
        for (Student student : set) {
            System.out.println(student);
        }
    }
}
class Student implements Comparable<Student> {
    private String name;
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    private int age;
    public Student() {    }
    private int score;

    @Override
    public int compareTo(Student o) {
        int x = o.getScore() - this.score;
        return x == 0 ? this.getName().compareTo(o.getName()) : x;
    }
}